﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;
using System.Text;

namespace CRM.Web.SurveyManage
{
    public partial class SurveyItemOption : Common.PageBase
    {
        public string ModifyUrl = "SurveyItemOption.aspx?AUTOID={0}&SURVEY_ID={1}&ITEM_ID={2}";
        T_SURVEY_BLL sBll = new T_SURVEY_BLL();
        T_SURVEY_ITEM_BLL iBll = new T_SURVEY_ITEM_BLL();
        T_SURVEY_ITEM_OPTION_BLL siopBll = new T_SURVEY_ITEM_OPTION_BLL();

        protected override void OnInit(EventArgs e)
        {
            base.VerificationRight = false;
            base.OnInit(e);
            gvList.PageSize = 10;
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlNEXT_ITEMBinding();
                pageDataInit();
                gvDataBinding();
            }
        }
        #region 数据绑定
        private void ddlNEXT_ITEMBinding()
        {
            ddlNEXT_ITEM.DataSource = iBll.GetList(String.Format(" AND SURVEY_ID='{0}' AND ITEM_ID<>'{1}' ", PageHelper.GetQueryString(this.Page, "SURVEY_ID"), PageHelper.GetQueryString(this.Page, "ITEM_ID")));
            ddlNEXT_ITEM.DataTextField = "ITEM_NAME";
            ddlNEXT_ITEM.DataValueField = "ITEM_ID";
            ddlNEXT_ITEM.DataBind();
            ddlNEXT_ITEM.Items.Insert(0, new ListItem("", ""));
        }
        #endregion

        private void pageDataInit()
        {
            String autoid = PageHelper.GetQueryString(this.Page, "AUTOID");
            if (!String.IsNullOrEmpty(autoid))
            {
                T_SURVEY_ITEM_OPTION_Entity siopEntity = new T_SURVEY_ITEM_OPTION_Entity();
                siopEntity = siopBll.GetEntity("AUTOID", StringHelper.ToInt(autoid));
                ddlNEXT_ITEM.SelectedValue = siopEntity.NEXT_ITEM.ToString();
                PageHelper.SetControlValue(this.Page, siopEntity);
            }
            else
            {
                txtSURVEY_ID.Text = PageHelper.GetQueryString(this.Page, "SURVEY_ID");
                txtITEM_ID.Text = PageHelper.GetQueryString(this.Page, "ITEM_ID");
            }
            T_SURVEY_Entity sEntity = new T_SURVEY_Entity();
            sEntity = sBll.GetEntity("SURVEY_ID", PageHelper.GetQueryString(this.Page, "SURVEY_ID"));
            if (sEntity != null)
                txtSURVEY_NAME.Text = sEntity.SNAME;
            T_SURVEY_ITEM_Entity iEntity = new T_SURVEY_ITEM_Entity();
            iEntity = iBll.GetList(String.Format(" AND SURVEY_ID='{0}' AND ITEM_ID='{1}'", PageHelper.GetQueryString(this.Page, "SURVEY_ID"),PageHelper.GetQueryString(this.Page, "ITEM_ID"))).FirstOrDefault();
            if (iEntity != null)
                txtITEM_NAME.Text = iEntity.ITEM_NAME;
        }


        /// <summary>
        /// 生成查询条件字符串
        /// </summary>
        private StringBuilder GenerationSQLString()
        {
            StringBuilder sql = new StringBuilder();
            if (!String.IsNullOrEmpty(Request.QueryString["SUREVY_ID"]))
            {
                sql.Append(" and SUREVY_ID = '%" + Request.QueryString["SUREVY_ID"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ITEM_ID"]))
            {
                sql.Append(" and ITEM_ID = '" + Request.QueryString["ITEM_ID"] + "'");
            }
            return sql;
        }

        /// <summary>
        /// 页面数据绑定
        /// </summary>
        private void gvDataBinding()
        {
            #region 查询条件字符串
            StringBuilder sql = GenerationSQLString();
            #endregion

            List<T_SURVEY_ITEM_OPTION_Entity> list;
            //获取总的记录条数
            gvList.RecordCount = (int)siopBll.GetCount(sql.ToString());
            list = siopBll.GetList(sql.ToString(), gvList.CurrentPageIndex * gvList.PageSize, gvList.PageSize, gvList.SortConditions);
            gvList.DataSource = list;
            gvList.DataKeyNames = new string[] { "AUTOID" };
            gvList.DataBind();
        }

        /// <summary>
        /// 新建问卷、题目
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string optionid = siopBll.GetMaxValue("OPTION_ID", String.Format(" AND SURVEY_ID='{0}' AND ITEM_ID='{1}'", txtSURVEY_ID.Text, txtITEM_ID.Text));
            int i = 1;
            if (!String.IsNullOrEmpty(optionid))
            {
                i = StringHelper.ToInt(optionid) + 1;
            }
            txtOPTION_ID.Text = i.ToString();
            txtOPTION_NAME.Text = "";
            ddlNEXT_ITEM.Text = "";
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtOPTION_NAME.Text))
            {
                PageHelper.ShowMessage(this.Page, "选项名称不能为空！");
                return;
            }
            if (ddlNEXT_ITEM.SelectedValue == txtITEM_ID.Text)
            {
                PageHelper.ShowMessage(this.Page, "当前题目的下一题不能为自己！");
                return;
            }
            T_SURVEY_ITEM_OPTION_Entity siopEntity = new T_SURVEY_ITEM_OPTION_Entity();
            siopEntity = siopBll.GetList(String.Format(" AND SURVEY_ID='{0}' and ITEM_ID='{1}' and OPTION_ID='{2}'", txtSURVEY_ID.Text, txtITEM_ID.Text, txtOPTION_ID.Text)).FirstOrDefault();
            if (siopEntity == null)
            {
                siopEntity = new T_SURVEY_ITEM_OPTION_Entity();
                siopEntity = siopBll.GetList(String.Format(" AND SURVEY_ID='{0}' and ITEM_ID='{1}' and OPTION_NAME='{2}'", txtSURVEY_ID.Text, txtITEM_ID.Text, txtOPTION_NAME.Text)).FirstOrDefault();
                if (siopEntity == null)
                {
                    siopEntity = new T_SURVEY_ITEM_OPTION_Entity();
                    PageHelper.GetControlValue(this.Page, ref siopEntity);
                    siopEntity.NEXT_ITEM = StringHelper.ToInt(ddlNEXT_ITEM.SelectedValue);
                    siopBll.Insert(siopEntity);
                }
                else
                {
                    PageHelper.ShowMessage(this.Page, String.Format("当前题目选项名称{0}已经存在，请核实后再进行增加！", txtOPTION_NAME.Text));
                    return;
                }
            }
            else
            {
                PageHelper.GetControlValue(this.Page, ref siopEntity);
                siopEntity.NEXT_ITEM = StringHelper.ToInt(ddlNEXT_ITEM.SelectedValue);
                siopBll.Update(siopEntity);
            }
            PageHelper.ShowMessage(this.Page, "保存成功！");
            Response.Redirect(HttpContext.Current.Request.RawUrl);
        }
        #region GridView 事件
        /// <summary>
        /// GridView 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            T_SURVEY_ITEM_OPTION_Entity entity = new T_SURVEY_ITEM_OPTION_Entity();
            entity.AUTOID = int.Parse((sender as GridView).DataKeys[e.RowIndex].Values[0].ToString());
            long rel = siopBll.Delete(entity);
            if (rel > 0)
            {
                Response.Redirect(HttpContext.Current.Request.RawUrl);
            }
            else
            {
            }
        }

        /// <summary>
        /// GridView 行数据绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string temp = string.Empty;
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {
                        GridView gv = (sender as GridView);
                        for (int i = 0; i < gv.Columns.Count; i++)
                        {
                            TemplateField tf = gv.Columns[i] as TemplateField;
                            if (tf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (tf.HeaderText)
                                {
                                    case "操作":
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;
                                    default:
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;
                                }
                                #endregion
                            }

                            BoundField bf = gv.Columns[i] as BoundField;
                            if (bf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (bf.DataField)
                                {
                                    case "NEXT_ITEM":
                                        try
                                        {
                                            temp = e.Row.Cells[i].Text.Trim();
                                            T_SURVEY_ITEM_Entity entity = new T_SURVEY_ITEM_Entity();
                                            entity = iBll.GetList(String.Format(" AND SURVEY_ID='{0}' AND ITEM_ID='{1}'", Request.QueryString["SURVEY_ID"], temp)).FirstOrDefault();
                                            if (entity != null)
                                                temp = entity.ITEM_NAME;
                                            e.Row.Cells[i].Text = temp;
                                        }
                                        catch (Exception)
                                        {

                                            //throw;
                                        }
                                        break;
                                    default:
                                        {

                                        }
                                        break;
                                }                                
                                #endregion
                            }
                        }
                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }
        }

        /// <summary>
        /// GridView 行创建事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {

                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }
        #endregion
    } 
}
